package com.ruoyi.houses.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.R;
import com.ruoyi.common.constant.HouseSaleStatus;
import com.ruoyi.common.core.domain.model.Broker;
import com.ruoyi.common.enums.HouseType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.SnowFlake;
import com.ruoyi.domain.Region;
import com.ruoyi.houses.domain.Houses;
import com.ruoyi.houses.domain.RentHouses;
import com.ruoyi.houses.domain.dto.request.HousesReqDTO;
import com.ruoyi.houses.mapper.HousesMapper;
import com.ruoyi.houses.mapper.RentHousesMapper;
import com.ruoyi.houses.service.IHousesService;
import com.ruoyi.houses.service.IRentHousesService;
import com.ruoyi.mapper.BrokerMapper;
import com.ruoyi.mapper.RegionMapper;
import com.ruoyi.plot.domain.dto.response.PlotInfoResDTO;
import com.ruoyi.plot.service.IPlotService;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class IRentHousesServiceImpl extends ServiceImpl<RentHousesMapper, RentHouses> implements IRentHousesService {


    @Resource
    IHousesService housesService;

    @Resource
    HousesMapper housesMapper;


    @Autowired
    RegionMapper regionMapper;

    @Autowired
    IPlotService plotService;

    @Autowired
    BrokerMapper brokerMapper;

    @Override
    @Transactional
    public R addRentHouses(HousesReqDTO housesReqDTO) {
        try {
            //校验是否为空！
            housesReqDTO.check();
            //转换实体
            Houses houses = BeanUtil.toBean(housesReqDTO, Houses.class);
            //状态为待租状态
            houses.setSaleStatus(HouseSaleStatus.RENTAGENCY.getStatus());
            SnowFlake snowFlake = new SnowFlake(1, 1);
            houses.setId(snowFlake.nextId());

            //添加房源区域
            Region region = regionMapper.selectById(houses.getCityId());
            houses.setCityName(region.getName());
            Region region1 = regionMapper.selectById(houses.getProvinceId());
            houses.setProvinceName(region1.getName());
            Region region2 = regionMapper.selectById(houses.getDistrictId());
            houses.setDistrictName(region2.getName());
            //查询经纪人电话
            Broker broker = brokerMapper.selectBrokerById(String.valueOf(housesReqDTO.getBrokerId()));
            //设置经纪人电话
            houses.setBrokerTel(broker.getMobile());
            // 获取当前登陆人
            Long userId = SecurityUtils.getUserId();
            houses.setCreateBy(String.valueOf(userId));

            PlotInfoResDTO plotInfoResDTO = plotService.selectPlotById(houses.getCommunityId());
            houses.setCommunityName(plotInfoResDTO.getCommunityName());

            //房源类型  租房
            houses.setHouseType(HouseType.RENT_HOUSE.getTypeId());

            //添加房屋
            housesService.insertHouses(houses);
            //房屋图片
            baseMapper.addImage(snowFlake.nextId(),housesReqDTO.getImageList());
            //房屋设施
            baseMapper.addFacilityId(snowFlake.nextId(),housesReqDTO.getFacilityId());
            //房屋详情
            RentHouses rentHouses = housesReqDTO.getRentHouses();
            rentHouses.setHouseId(snowFlake.nextId());
            baseMapper.addHousesDisc(housesReqDTO.getRentHouses());

        } catch (Exception e) {
            // 处理异常，例如记录日志并返回错误响应
            e.printStackTrace();
        }

        return R.OK();
    }
}
